var fun = new (function(){
	
	/**
	 * 地址跳转
	 * @param url
	 */
	this.href = function(url){
		location.href=url;
	};
	
	/**
	 * 刷新当前页
	 */
	this.reload = function(){
		location.reload(true);
	};
	
	/**
	 * 防止冒泡事件<br>
	 * 在需要防止冒泡事件的标签上增加onclick="fun.cancelBubble();"
	 */
	this.cancelBubble = function() {
		var theEvent = window.event || arguments.callee.caller.arguments[0];
		theEvent.cancelBubble = true;
		return false;
	};
	
	/**
	 * 超长内容简略显示
	 */
	this.getLimitLenStr = function(str, len) {
		if (!len) {
			len = 50;
		}
		var l = str.length;
		var n = 0;
		var resultStr = str;
		for ( var i = 0; i < l; i++) {
			if (str.charCodeAt(i) < 0 || str.charCodeAt(i) > 255) {
				n += 2;
			} else {
				n++;
			}
			if (n > len) {
				resultStr = str.substring(0, i) + "...";
				break;
			}
		}
		return resultStr;
	};
	
	/**
	 * 取得日期时间字符串的毫秒值
	 * 
	 * @param datetimeStr
	 * @returns
	 */
	this.toTimestamp = function(datetimeStr) {
		if (!datetimeStr) {
			return "";
		}
		var datetimeArr = datetimeStr.split(" ");
		var dateArr = datetimeArr[0].split("-");
		var timeArr = datetimeArr[1].split(":");
		var date = new Date();
		date.setFullYear(dateArr[0], dateArr[1]-1, dateArr[2]);
		date.setHours(timeArr[0], timeArr[1], timeArr[2], 0);
		return date.getTime();
	};
	
	/**
	 * 根据日期对象或者毫秒值转换成日期时间字符串
	 * @param date
	 * @returns {String}
	 */
	this.toDatetime = function(date){
		if (!date) {
			return "";
		}
		if (false) {// TODO 有待处理
			return toDatetimeStr(date);
		} else {
			var d = new Date();
			d.setTime(date);
			return toDatetimeStr(d);
		}
		
	};

	/**
	 * 将毫秒值转换为日期时间字符串形式
	 * @param timeStamp
	 * @returns {String}
	 */
	this.getDatetimeName = function(timeStamp){
		var date = new Date();
		date.setTime(timeStamp);
		var year = date.getFullYear();
		var mon = date.getMonth();
		var dat = date.getDate();
		var hour = date.getHours();
		var min = date.getMinutes();
		var sec = date.getSeconds();
		var curDate  = new Date();
		var curYear = curDate.getFullYear();
		var curMon = curDate.getMonth();
		var curDat = curDate.getDate();
		var curHour  = curDate.getHours();
		var curMin = curDate.getMinutes();
		var str = "";
		
		if(curYear-year ==0){
			if(curMon-mon >1){
				str +=(mon+1)+"月"+dat+"日";
			}
			else if(curMon-mon ==1){
				str +="上月"+dat+"日";
			}
			else{
				if(curDat-dat==0){
					str +="今天";
				}
				else if(curDat-dat==1){
					str +="昨天";
				}
				else if(curDat-dat==2){
					str +="前天";
				}
				else{
					str +=dat+"日";
				}
			}
		}
		else{
			if(curYear-year>2){
				str += year+"年";
			}
			else if(curYear-year ==2){
				str += "前年";
			}
			else if(curYear-year ==1){
				str += "去年";
			}
			
			str +=(mon+1)+"月"+dat+"日";
			
		}
		if(hour>18){
			str +="晚上"+(hour-12)+"点";
		}
		else if(hour>12){
			str +="下午"+(hour-12)+"点";
		}
		else if(hour==12){
			str +="中午"+hour+"点";
		}
		else if(hour>6){
			str +="上午"+hour+"点";
		}
		else{
			str +="零晨"+hour+"点";
		}
		if(min==30){
			str +="半";
		}
		else if(min==0){
			str +="整";
		}
		else{
			str +=min+"分";
		}
		return str;
	};
	/**
	 * 时间长度描述
	 * @param dStamp 两时间毫秒差
	 * @returns {String}
	 */
	this.getTimeLenName = function(dStamp){
		var str="";
		var a = 1000*60*60*24*31;
		var monCount = Math.floor(dStamp/a);
		var b = a/31;
		var c = dStamp%a;
		var dayCount = Math.floor(c/b);
		var e = b/24;
		var f = c%b;
		var hourCount = Math.floor(f/e);
		var g = e/60;
		var h = f%e;
		var minCount = Math.floor(h/g);
		var i = g/60;
		var j = h%g;
		var secCount = Math.floor(j/i);
		if(monCount>0){
			str+=monCount+"个月";
		}
		if(dayCount>0){
			str+=dayCount+"天";
		}
		if(hourCount>0){
			str+=hourCount+"小时";
		}
		if(minCount>0){
			str+=minCount+"分";
		}
		if(secCount>0){
			str+=secCount+"秒";
		}
		return str;
	};
	
	/**
	 * 根据日期对象取得日期时间字符串值
	 * @param date
	 * @returns {String}
	 */
	function toDatetimeStr(date){
		var str="";
		var year = date.getFullYear();
		var mon = date.getMonth()+1;//
		var dat = date.getDate();
		var hour = date.getHours();
		var min = date.getMinutes();
		var sec = date.getSeconds();
		return year+"-"+(mon<10?("0"+mon):mon)+"-"+(dat<10?("0"+dat):dat)+
		" "+(hour<10?("0"+hour):hour)+":"+(min<10?("0"+min):min)+":"+(sec<10?("0"+sec):sec);
	};
})();




/**
 * @author mo_yq5@163.com
 * @returns {Function}
 */
function $Function(){
	
}
/**
 * 根据值返回JQuery类型的复选框对象
 * @param fieldNameP
 * @param valueP
 * @returns
 */
$Function.prototype.get$CheckboxByValue = function(fieldNameP,valueP){
	return this.get$ElByN(fieldNameP).filter(":checkbox:[value='"+valueP+"']");
};
/**
 * 全选
 * @param fieldNameP
 * @returns
 */
$Function.prototype.selectAll=function(fieldNameP){
	$("[name='"+fieldNameP+"']").attr("checked",true);
};

/**
 * 反选
 * @param fieldNameP
 * @returns
 */
$Function.prototype.selectInvert=function(fieldNameP){
	$("[name='"+fieldNameP+"']").each(function(index,el){
		$(el).attr("checked",!$(el).attr("checked"));
	});
};
/**
 * 获取单选值
 * @param fieldNameP 单选框name
 * @returns
 */
$Function.prototype.getRadioValue=function(fieldNameP){
	return this.get$ElByNamne(fieldNameP).filter(":radio:checked").attr("value");
	
};
/**
 * 给表单赋值
 * @param fieldIdP 表单项id
 * @param valueP
 */
$Function.prototype.setFieldValue=function(fieldIdP,valueP){
	this.get$ElById(fieldIdP).attr("value",valueP);
};

/**
 * 
 * @param fieldNameP
 * @returns
 */
$Function.prototype.get$ElByName=function(fieldNameP){
	return $("[name='"+fieldNameP+"']");
};
/**
 * 根据表单id属性值返回JQuery类型的表单对象
 * @param fieldIdP
 * @returns
 */
$Function.prototype.get$ElById=function(fieldIdP){
	return $("#"+fieldIdP);
};
/**
 * 返回将值以strP连接得的字符串
 * @param $elsP
 * @param strP
 * @returns
 */
$Function.prototype.joinValue = function($elsP,strP){
	strP=strP?strP:",";
	return $elsP.map(function(){
		  return $(this).attr("value");
	}).get().join(strP);
};
/**
 * 获取表单值
 * @param fieldIdP 表单项id
 * @returns
 */
$Function.prototype.getFieldValue=function(fieldIdP){
	return this.get$ElById(fieldIdP).attr("value");
};

/**
 * 返回复选框选中值的“,”连接形式的字符串
 * @param fieldNameP
 * @returns
 */
$Function.prototype.getCheckboxValue=function(fieldNameP){
	return this.joinValue(this.get$ElByName(fieldNameP).filter(":checkbox:checked"));
};

/**
 * 根据提供的值，使相应的复选框选中
 * @param fieldNameP 复选框名
 * @param valueP	需要选中的复选框的值
 */
$Function.prototype.setChecked=function(fieldNameP,valueP){
	this.get$CheckboxByValue(fieldNameP,valueP).attr("checked",true);
};

/**
 * 根据提供的值，使相应的复选框取消选中
 * @param fieldNameP 复选框名
 * @param valueP	需要取消选中的复选框的值
 */
$Function.prototype.setUnChecked=function(fieldNameP,valueP){
	this.get$CheckboxByValue(fieldNameP,valueP).attr("checked",false);
};
var $F = new $Function();
